package com.iplant.ams.controller.dms.maintenance;

import com.iplant.ams.service.DRMService;
import com.iplant.ams.service.po.dms.maintenance.DMSMaintenanceRule;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/DMSMaintenanceRule")
public class DMSMaintenanceRuleController extends BasicController<DMSMaintenanceRule> {
    private static Logger logger = LoggerFactory.getLogger(DMSMaintenanceRuleController.class);

    @Autowired
    DRMService mDRMService;

    public DMSMaintenanceRuleController() {
        super(DMSMaintenanceRule.class);
    }

    /**
     * 查询待竣工确认过滤项
     */
    @GetMapping("/QueryMaintenanceRuleByMaintenanceItemID")
    public Object QueryMaintenanceRuleByMaintenanceItemID(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wMaintenanceItemID = StringUtils.parseInt(request.getParameter("MaintenanceItemID"));
            int wType = StringUtils.parseInt(request.getParameter("Type"));
            String wDescription = StringUtils.parseString(request.getParameter("Description"));
            int wActive = request.getParameter("Active").equals("") ? -1 : StringUtils.parseInt(request.getParameter(
                    "Active"));

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<DMSMaintenanceRule>> wServiceResult =
                    mDRMService.DMS_QueryMaintenanceRuleByMaintenanceItemID(wLoginUser, wMaintenanceItemID, wType,
                            wDescription, wActive, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 保存规则
     */
    @Override
    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            DMSMaintenanceRule wDMSMaintenanceRule = CloneTool.Clone(wParam.get("data"), DMSMaintenanceRule.class);
            if (wDMSMaintenanceRule == null) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mDRMService.DMS_UpdateMaintenanceRule(wLoginUser,
                    wDMSMaintenanceRule);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }
}
